﻿using System;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.IO;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        /**
         *  AES Key (16 bytes)
         */
        private static readonly byte[] AES_KEY = 
        {
            0x38, 0x58, 0xF6, 0x22, 0x30, 0xAC, 0x3C, 0x91, 0x5F, 0x30, 0x0C, 0x66, 0x43, 0x12, 0xC6, 0x3F
        };
        /**
         *  AES Initial Vector (16 bytes)
         */
        private static readonly byte[] AES_IV = 
        {
            0xDC, 0xE9, 0xB4, 0xBB, 0x2A, 0xE0, 0x2D, 0xF9, 0xB8, 0xB8, 0xB9, 0x51, 0x93, 0x76, 0x2E, 0xB8
        };

        protected _Default()
        {
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected byte[] AES_cbc_Encrypt(string data)
        {
            RijndaelManaged aesAlg = null;
            MemoryStream mStream = null;
            CryptoStream cStream = null;
            StreamWriter writer = null;
            try
            {
                aesAlg = CreateAESAlgorithm();
                mStream = new MemoryStream();
                cStream = new CryptoStream(mStream, aesAlg.CreateEncryptor(), CryptoStreamMode.Write);
                writer = new StreamWriter(cStream);
                writer.Write(data);
            }
            finally
            {
                writer.Close();
                cStream.Close();
                mStream.Close();
                aesAlg.Clear();
            }
            return mStream.ToArray();
        }

        protected string AES_cbc_Decrypt(byte[] encdata)
        {
            RijndaelManaged aesAlg = null;
            MemoryStream mStream = null;
            CryptoStream cStream = null;
            StreamReader reader = null;

            try
            {
                aesAlg = CreateAESAlgorithm();
                mStream = new MemoryStream(encdata);
                cStream = new CryptoStream(mStream, aesAlg.CreateDecryptor(), CryptoStreamMode.Read);
                reader = new StreamReader(cStream);
                return reader.ReadToEnd();
            }
            finally
            {
                reader.Close();
                cStream.Close();
                mStream.Close();
                aesAlg.Clear();
            }
        }

        private RijndaelManaged CreateAESAlgorithm()
        {
            RijndaelManaged aes = new RijndaelManaged();
            aes.Key = AES_KEY;
            aes.IV = AES_IV;
            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.Zeros;
            return aes;
        }

        protected byte[] HexStringToBytes(string hexstr)
        {
            List<byte> bytes = new List<byte>();
            for (int i = 0; i < hexstr.Length; i = i + 2)
            {
                bytes.Add(byte.Parse(hexstr.Substring(i, 2), System.Globalization.NumberStyles.HexNumber));
            }
            return bytes.ToArray();
        }

        protected string BytesToHexString(byte[] bytes)
        {
            return BitConverter.ToString(bytes).Replace("-", "");
            /* XXX
                StringBuilder result = new StringBuilder();
                foreach (byte b in bytes)
                {
                    result.Append(b.ToString("x2"));
                }
                return result;
            */
        }

        protected byte[] ComputeHashMD5(byte[] bytes)
        {
            return new MD5CryptoServiceProvider().ComputeHash(bytes);
        }
    }
}
